﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZS_PGFBSQControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            CustomGridView CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            if (!base.Page.IsPostBack)
            {
                Bind();
                string temp = ckComType();
                if (!string.IsNullOrEmpty(temp))
                {
                    if (temp == "评估机构")
                    {
                        CustomGridList1.Columns[9].Visible = true;
                        CustomGridList1.Columns[10].Visible = true;
                    }
                }
            }
            IsZH();
            CustomGridList1.OnLoadData += new EventHandler(CustomGridList1_OnLoadData);
            Button btnsearch = (Button)FindControl("btnSeach");
            btnsearch.Click += new EventHandler(Go_Search);
            foreach (GridViewRow gvr in CustomGridList1.Rows)
            {
                Button btn_tj = (Button)gvr.FindControl("btn_tj");
                Button btn_tj1 = (Button)gvr.FindControl("btn_tj1");
                Button btn_tj2 = (Button)gvr.FindControl("btn_tj2");
                btn_tj.Click += new EventHandler(ChangeStatus);
                btn_tj1.Click += new EventHandler(ChangeStatus1);
                btn_tj2.Click += new EventHandler(ChangeStatus2);
            }
            Button btn_insert = (Button)FindControl("btn_insert");
            btn_insert.Click += new EventHandler(btn_insert_Click);

            if (Page.Request["pnid"]!=null)
            {
                string sql = "Select * from zs_pgfbsq where pnid = '" + Page.Request["pnid"] + "'";
                DataTable dt = _EluZSDataProvider.GetTable(sql);
                if (dt.Rows.Count>0)
                {
                   
                    btn_insert.Enabled = false;
                }
            }
        }

        protected void btn_insert_Click(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "insert", "<script>javascript:window.location.href=('ZS_PGFBSQAdd.aspx?pnid="+Page.Request["pnid"]+"');</script>");
        }
        /// <summary>
        /// 是否是公示后3天
        /// </summary>
        private void IsZH()
        {
            //string sql = "Select id,记录当前时间  from ZS_PGFBSQ where 提交状态 = 0 and pnid = '" + Page.Request["PNID"] + "'";
            string sql = "Select id,记录当前时间  from ZS_PGFBSQ where 提交状态 = 0 and pnid in (Select Id From zs_prono Where pid = '" + Page.Request["PNID"] + "')";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count>0)
            {
                for (int i = 0; i < dt.Rows.Count;i++)
                {
                    if (!string.IsNullOrEmpty(dt.Rows[i]["记录当前时间"].ToString().Trim()) && dt.Rows[i]["记录当前时间"].ToString().Trim() != "")
                    {
                        int Day1 = Convert.ToDateTime(dt.Rows[i]["记录当前时间"]).Day;
                        int Day2 = System.DateTime.Now.Day;
                        if (Day2 >= (Day1+3))
                        {
                            string upsql = "update ZS_PGFBSQ set 提交状态 = 1  where  pnid in (Select Id From zs_prono Where pid = '" + Page.Request["PNID"] + "')";
                            _EluZSDataProvider.RunSqlNon(upsql);
                        }
                    }
                }
            }
        }
        protected string ckComType()
        {
            string dwlb = string.Empty;
            if (!string.IsNullOrEmpty(userid))
            {
                string cid = _EluZSDataProvider.GetSomeStr("zs_user_company", "cid", "userid", userid);
                if (!string.IsNullOrEmpty(cid)) { dwlb = _EluZSDataProvider.GetSomeStr("zs_company", "单位类别", "id", cid); }
            } return dwlb;
        }
        protected void Go_Search(object sender, EventArgs e)
        {
            Bind();
        }
        protected void Bind()
        {
            string strpnid = base.Page.Request["PNID"];
            CustomGridView CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            CustomGridList1.RowCreated += new GridViewRowEventHandler(CustomGridList1_RowCreated);
            CustomGridList1.RowDataBound += new GridViewRowEventHandler(CustomGridList1_RowDataBound);
            DataTable dt = GetPGJGBMXX("0",strpnid, GetSqlWhere1(),GetSqlWhere2());
            CustomGridList1.DataSource = dt;
            CustomGridList1.RecordCount = dt.Rows.Count;
            CustomGridList1.PageSize = 10;
            CustomGridList1.DataKeyNames = new string[] { "ID", "提交状态", "报名状态" };
            CustomGridList1.DataBind();
            
        }

        public DataTable GetPGJGBMXX(string tt, string pnid, string strwhere1, string strwhere2)
        {
            string sql = string.Empty;
            if (tt == "1")
            {
                sql = string.Format(@"select t.id,t.pnid,t.申请单位,t.经办人,to_char(t.申请时间,'yyyy-mm-dd') 申请时间,
(select 立项号 from zs_project where id in (select pid from zs_prono where id=t.pnid) and rownum<2) 立项号,
(select 项目名称 from zs_project where id in (select pid from zs_prono where id=t.pnid) and rownum<2) 项目名称,
t.提交状态,{0} 
(select id bmid from ZS_PGBMFBXX where sid=t.id) bmid   
from zs_pgfbsq t where 提交状态=1 and pnid ='{1}'", strwhere1, pnid);
            }
            else if (tt == "0")
            {
                sql = string.Format(@"select t.id,t.pnid,t.申请单位,t.经办人,to_char(t.申请时间,'yyyy-mm-dd') 申请时间,
(select 立项号 from zs_project where id in t.pnid and rownum<2) 立项号,
(select 项目名称 from zs_project where id in t.pnid and rownum<2) 项目名称,
t.提交状态,{0} 
(select id bmid from ZS_PGBMFBXX where sid=t.id and rownum<2) bmid   
from zs_pgfbsq t where pnid ='{1}'", strwhere1, pnid);
            }
            if (!string.IsNullOrEmpty(strwhere2)) { sql += strwhere2; }
            sql += " order by t.申请时间 desc";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            return dt;
        }


        protected void CustomGridList1_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }
        protected void CustomGridList1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            CustomGridView CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            foreach (GridViewRow gvr in CustomGridList1.Rows)
            {
                if ( CustomGridList1.DataKeys[gvr.RowIndex].Values["提交状态"].ToString() == "0")
                {
                    Button btn = (Button)gvr.FindControl("btn_tj");
                    btn.Enabled = true;
                }
                else
                {
                    Button btn2 = (Button)gvr.FindControl("btn_tj2");
                    btn2.Enabled = true;
                }
                if ( CustomGridList1.DataKeys[gvr.RowIndex].Values["报名状态"].ToString() == "0")
                {
                    Button btn1 = (Button)gvr.FindControl("btn_tj1");
                    string sid = CustomGridList1.DataKeys[gvr.RowIndex].Values["ID"].ToString();
                    string tt = _EluZSDataProvider.GetSomeStr("zs_pgbmfbxx", "count(*)", "sid", sid);
                    if (!string.IsNullOrEmpty(tt)) { if (Convert.ToInt32(tt) > 0) { btn1.Enabled = true; } else { btn1.Enabled = false; } }
                }
            }
        }
        protected void CustomGridList1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView rowview = (DataRowView)e.Row.DataItem;
                string sID = ((HiddenField)e.Row.FindControl("HiddenField1")).Value;
                string bMID = ((HiddenField)e.Row.FindControl("HiddenField2")).Value;
                string bmzt = rowview["提交状态"].ToString();
                if (bmzt == "1")
                {
                    e.Row.Cells[9].Text = "报名信息(已公开)";
                    e.Row.Cells[9].Attributes.Add("style", "cursor:pointer;color:blue;");
                    e.Row.Cells[9].Attributes.Add("onclick", "javascript:window.location.href=('ZS_PGBMXX.aspx?ID=" + bMID + "&SID=" + sID + "&PNID=" + base.Page.Request["PNID"] + "');");
                }
            }
        }

        protected string GetSqlWhere1() {
            string hqcid = _EluZSDataProvider.GetSomeStr("zs_user_company", "cid", "userid", userid);
            string strwhere = string.Empty;
            if (!string.IsNullOrEmpty(hqcid))
            { //strwhere = " and COMID='" + hqcid + "'";
                strwhere = "to_number(nvl((select distinct 报名状态 from ZS_PGBMJL where sid=t.id and COMID='" + hqcid + "'),0)) 报名状态,to_number(nvl((select distinct 通过状态 from ZS_PGBMJL where sid in t.id and COMID='" + hqcid + "'),0)) 通过状态,";
            }
            else {
                strwhere = "'' 报名状态,'' 通过状态,";
            }
            return strwhere;
        }
        protected string GetSqlWhere2()
        {
            string sqlWhere = string.Empty;
            TextBox txtCName = (TextBox)FindControl("txtCName");
            TextBox txtJBR = (TextBox)FindControl("txtJBR");
            string strCName = txtCName.Text;
            string strJBR = txtJBR.Text;

            if (!string.IsNullOrEmpty(strCName))
            {
                sqlWhere += " and t.申请单位 like '%" + strCName + "%'";
            }
            if (!string.IsNullOrEmpty(strJBR))
            {
                sqlWhere += " and t.经办人 like '%" + strJBR + "%'";
            }
            return sqlWhere;
        }


        protected void ChangeStatus(object sender, EventArgs e)
        {
            CustomGridView CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string sID = CustomGridList1.DataKeys[gvr.RowIndex].Values["ID"].ToString();
            bool ckdata = _EluZSDataProvider.CkSomStr("ZS_PGBMFBXX", "sid", sID);
            if (ckdata)
            {
                bool Change = _EluZSDataProvider.ChangeZT("zs_pgfbsq", "提交状态", sID, "1");
                if (Change)
                {
                    Bind();
                }
            }
            else
            {
                base.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('公示的报名信息还未填写！');</script>");
                Bind();
            }
        }
        protected void ChangeStatus1(object sender, EventArgs e)
        {
            CustomGridView CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string sID = CustomGridList1.DataKeys[gvr.RowIndex].Values["ID"].ToString();
            string cID = _EluZSDataProvider.GetSomeStr("zs_user_company", "cid", "userid", userid);
            bool ckbmry = _EluZSDataProvider.CkSomStr("zs_user_company", "userid", userid);
            if (ckbmry)
            {
                _EluZSDataProvider.InsertPGJGBMXX(base.Page.Request["PNID"], cID, sID);
                Bind();
            }
            else
            {
                base.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('当前登录用户未关联公司！');</script>");
            }
        }
        protected void ChangeStatus2(object sender, EventArgs e)
        {
            CustomGridView CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string sID = CustomGridList1.DataKeys[gvr.RowIndex].Values["ID"].ToString();
            bool change = _EluZSDataProvider.ChangeZT("zs_pgfbsq", "提交状态", sID, "0");
            if (change) { Bind(); }
        }
    }
}


